package gw.com.android.ui.news;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gwtsz.gts2.hx.R;

import java.util.List;

import gw.com.android.presenter.BackTopPresenter;
import gw.com.android.presenter.http.StrategyRequest;
import www.com.library.app.Logger;
import www.com.library.interfaces.ReqCallBack;
import www.com.library.util.DeviceUtil;
import www.com.library.view.XRecyclerView;

/**
 * @author jett
 * @since 2018-08-30.
 */
public class StrategyFragment extends Fragment {

    private boolean viewCreated = false;
    private boolean lazyLoaded = false;
    private View emptyView;
    private XRecyclerView rvList;
    private StrategyAdapter mAdapter;
    private StrategyRequest mRequest;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_strategy, container, false);
        initView(rootView);
        viewCreated = true;
        onLazyLoad();
        return rootView;
    }

    private void initView(View rootView) {
        rvList = rootView.findViewById(R.id.strategy_list);
        emptyView = rootView.findViewById(R.id.strategy_empty);
        rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        rvList.setAdapter(new EmptyAdapter());
        int x = DeviceUtil.instance().dip2px(15, getActivity());
        int y = DeviceUtil.instance().dip2px(36, getActivity());
        new BackTopPresenter(getActivity(), rvList).location(Gravity.BOTTOM | Gravity.RIGHT, x, y).calcNavigationBar(true);
        rvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadFromNet();
            }

            @Override
            public void onLoadMore() {
                nextPage();
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!lazyLoaded && isVisibleToUser) {
            onLazyLoad();
        }
    }

    private void onLazyLoad() {
        if (viewCreated && getUserVisibleHint()) {
            lazyLoaded = true;
            Logger.i("strategy", "StrategyFragment lazy load");
            if (mRequest == null) {
                mRequest = new StrategyRequest();
            }
            List<StrategyRequest.ArticleData> data = mRequest.fetchLocal();
            if (data != null && !data.isEmpty()) {
                mAdapter = new StrategyAdapter(getActivity());
                rvList.setAdapter(mAdapter);
                mAdapter.update(data, true);
            }
            rvList.setRefreshing(true);
        }
    }

    private void loadFromNet() {
        if (mRequest == null) {
            mRequest = new StrategyRequest();
        }
        mRequest.getList(new ReqCallBack<List<StrategyRequest.ArticleData>>() {
            @Override
            public void onReqSuccess(List<StrategyRequest.ArticleData> result) {
                rvList.refreshComplete();
                refreshUi(result);
            }

            @Override
            public void onReqFailed(String errorMsg) {
                rvList.refreshComplete();
                refreshUi(null);
            }
        });
    }

    private void nextPage() {
        mRequest.nextPage(new ReqCallBack<List<StrategyRequest.ArticleData>>() {
            @Override
            public void onReqSuccess(List<StrategyRequest.ArticleData> result) {
                mAdapter.append(result);
                rvList.loadMoreComplete();
                rvList.setNoMore(!mRequest.hasNextPage());
            }

            @Override
            public void onReqFailed(String errorMsg) {
                rvList.loadMoreComplete();
            }
        });
    }

    private void refreshUi(List<StrategyRequest.ArticleData> result) {
        if (mAdapter == null) {
            mAdapter = new StrategyAdapter(getActivity());
            rvList.setAdapter(mAdapter);
        }
        if (result == null || result.isEmpty()) {
            if (mAdapter.getItemCount() <= 0) {
                emptyView.setVisibility(View.VISIBLE);
                rvList.hideFooter();
            }
        } else {
            if (emptyView.getVisibility() == View.VISIBLE) {
                emptyView.setVisibility(View.GONE);
            }
            mAdapter.update(result, true);
            rvList.setNoMore(!mRequest.hasNextPage());
        }
    }

    private class EmptyAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_strategy_empty, parent, false);
            return new RecyclerView.ViewHolder(itemView) {
            };
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        }

        @Override
        public int getItemCount() {
            return 10;
        }

    }

}
